package org.zxp.algorithm.sort;

import org.junit.Test;
import org.zxp.algorithm.sort.utils.Tools;

/**
 * @program: algorithm
 * @description: 冒泡排序实现
 * 特点还是相邻两个比，值大（或小）的交换，每次比的元素不一定
 * @author: X-Pacific zhang
 * @create: 2019-04-07 10:36
 **/
public class MaopaoSort {
    @Test
    public void testMname() throws Exception {
        int[] oraArray = {5,2,7,4,8,7};
        long time1 = System.currentTimeMillis();
        oraArray = Tools.gen(1000000);
        long time2 = System.currentTimeMillis();
        int[] resultArray = bubbleSort(oraArray);
        long time3 = System.currentTimeMillis();
        Tools.printResult(resultArray, time1, time2, time3,"冒泡");
    }

    private int[] bubbleSort(int[] orgArray) throws Exception {
        for (int i = 1; i <= orgArray.length; i++) {
            //5个元素
            //内圈 0比1 1比2 2比3 3比4 这时会把4锁定住
            //内圈 0比1 1比2 2比3 这时不用比4，4已经最大，把3锁定住
            //结合如上规律，内圈每次会少比一次，最大的那个不用比了
            for (int j = 0; j < orgArray.length - i; j++) {
                if(orgArray[j] >= orgArray[j+1]){
                    Tools.change(orgArray,j,j+1);
                }
            }
        }
        return orgArray;
    }

}
